package com.knightsoft.desktopapp.jchatsystem;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.metal.MetalLookAndFeel;

import java.io.PrintStream;
import java.util.Date;

public class ChatServer {

	/**
	 * @author : Michael
	 * @since : Nov/4/2008 
	 */
	public static void main(String[] args) {
		
		UIManager.put("swing.boldMetal", Boolean.FALSE);
        JDialog.setDefaultLookAndFeelDecorated(true);
        JFrame.setDefaultLookAndFeelDecorated(true);

        try {
            UIManager.setLookAndFeel(new MetalLookAndFeel());
        }  
        catch ( UnsupportedLookAndFeelException e ) {
            System.out.println ("Metal Look & Feel not supported on this platform. \nProgram Terminated");
            System.exit(0);
        }
        
		if ( args.length > 1 ) 
			printUsage();
		//default start with GUI
		else if ( args.length == 0 )
			new ChatServerGUI().launchFrame();
		else {
			String input = args[0].trim();
	
			if (input.toUpperCase().equals("-GUI")) {
				new ChatServerGUI().launchFrame();
			}
			else if (input.toUpperCase().equals("-CLI")) {
				System.out.println("Start Server ...");
				new ChatServerCore().start();
			}
			else
				printUsage();
		}
	}
	
	/*
	 * @return : the usage of ChatServer
	 */
	private static void printUsage() {
		System.out.println("Usage : ChatServer [-GUI] [-CLI] \n\n" +
						   "Options :  \n" +
						   "-GUI   Start the Chat Server GUI   \n" +
						   "-gui   The same as -GUI            \n" +
						   "-CLI   Start the Chat Server from Command line, no graphics interface   \n" + 
						   "-cli   The same as -CLI             ");
		System.exit(0);
	}

}

@SuppressWarnings("serial")
/*
 * ChatServer的图形界面包装类.根据调用参数选择命令行方式启动或者显示图形界面
 */
class ChatServerGUI extends JFrame {
	
	public static final int WINDOW_WIDTH  = 300;
	public static final int WINDOW_HEIGHT = 500;
	
	private PrintStream pOut = null;
	private PrintStream pDefault = System.out;
	private JScrollPane jsp = null;
	private JTextArea txtInfo = new JTextArea();
	/*
	private JTextField txtPort = new JTextField();
	private JLabel lblPort = new JLabel("Port: ");
	private JTextField txtHost = new JTextField();
	private JLabel lblHost = new JLabel("Host: ");
	*/
	
	// draw the main UI
	public void launchFrame() {
		setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
		setTitle("Chat Server V2");
		
		//ImageIcon icon = new ImageIcon("res/network.png");
		ImageIcon icon = new ImageIcon(ChatServer.class.getResource("/network.png"));
		setIconImage(icon.getImage());
		setResizable(false);
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.setOut(pDefault);
				setVisible(false);
				System.exit(0);
				//shutdown();
			}
		});
		
		txtInfo.setAutoscrolls(true);
		txtInfo.setLineWrap(true);
		
		jsp = new JScrollPane(txtInfo, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, 
								ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		add(jsp, BorderLayout.CENTER);

		setVisible(true);
		initServer();
	}
	
	private void initServer() {
		// Reset the output stream to TextArea.
		pOut = new PrintStream(System.out) {
			public void println(String x) {
				txtInfo.append(x + "\n");
			}
			
			public void print(String x) {
				txtInfo.append(x + "\n");
			}
		};
		System.setOut(pOut);  
		
		System.out.println("Server started @" + new Date());
		System.out.println("*********************************************************");
		new ChatServerCore().start();
	}
	
}
